<?
/**
 * Update a record.
 *
 * @params    record        The new value.
 */

#$params["db_id"]=$this->id;
#$record = $this->db->autoprepare($params);

$fields = $this->fields;

$mode = "_update";
foreach ($fields as $k=>$f) {
    if (isset($f[$mode]))
    foreach ($f[$mode] as $kk=>$vv)
        $fields[$k][$kk]=$vv;
    }

$oe = array();
foreach ($fields as $k=>$f) {
    @$oe[$k]=$params["record"][$k];
    }

$e = $oe;
foreach ($fields as $k=>$f) {
    if (isset($f["special"]) && $f["special"])
        unset($e[$k]);
    }

$_e = $this->db->query1("select * from `{$this->table}` where {$this->key} = ".$this->db->sqlint($this->id));
$e[$this->key]=$this->id;

$changed = 0;
foreach ($e as $k=>$v) {
	if ((!isset($_e[$k]) && $v != "") || $_e[$k] != $v) { $changed ++; }
	}
if ($changed) {
	$this->db->autoquery("update",$this->table,$e);
	}

$e = $oe;
foreach ($fields as $k=>$f) {
    if (isset($f["special"]) && $f["special"]) {
        $p = $f;
        $p["name"]=$k;
        $p["value"]=$e[$k];
        $p["record"] = $e;
        $p["_record"] = $_e;

        $type = $f["type"];
        $inc = isset($f["inc"])?$f["inc"]:"fields3/{$type}_update.php";
        if ($this->__exists($inc)) $this->__inc($inc,$p);
        }
    }
?>
